package cn.itcast.gjp.dao;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import cn.itcast.gjp.domain.ZhangWu;
import cn.itcast.gjp.tools.JDBCUtils;

public class ZhangWuDao {
	private QueryRunner qr = new QueryRunner(JDBCUtils.getSource());
	public boolean deleteZhangWu(int zwid){
		try {
			String sql="DELETE FROM gjp_zhangwu WHERE zwid=?;";
			int i = qr.update(sql,zwid);
			if(i!=0){
				return true;
			}else{
				return false;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	public boolean editZhangWu(ZhangWu zw) {
		try {
			String sql = "UPDATE  gjp_zhangwu SET flname=? ,money=?,zhanghu=?,"
					+ "createtime=?,description=? WHERE zwid=?;";
			Object[] params = { zw.getFlname(),zw.getMoney(),zw.getZhanghu(),zw.getCreatetime(),zw.getDescription(),zw.getZwid() };
			int i = qr.update(sql, params);
			if (i != 0) {
				return true;
			} else {
				return false;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	public boolean addZhangWU(ZhangWu zw) {
		try {
			String sql = "INSERT INTO gjp_zhangwu (flname,money,zhanghu,createtime,description) "
					+ "VALUES (?,?,?,?,?);";
			Object[] params = { zw.getFlname(),zw.getMoney(),zw.getZhanghu(),zw.getCreatetime(),zw.getDescription() };
			int update = qr.update(sql, params);
			if (update != 0) {
				return true;
			} else {

				return false;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	public List<ZhangWu> selectAll() {
		try {
			String sql = "SELECT * FROM gjp_zhangwu;";
			List<ZhangWu> list = qr.query(sql, new BeanListHandler<>(ZhangWu.class));
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	public List<ZhangWu> select(String startDate, String endDate) {
		try {
			String sql = "SELECT * FROM gjp_zhangwu WHERE createtime BETWEEN ? AND ?; ";
			Object[] params = { startDate, endDate };
			List<ZhangWu> list = qr.query(sql, new BeanListHandler<>(ZhangWu.class), params);
			return list;
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

}
